package blue.mqtt4j.internal.client.handler;

import blue.mqtt4j.internal.client.net.NettyClient;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Jin Zheng
 * @since 1.0 2020-10-26
 */
public class PubRecMessageHandler implements MessageHandler<MqttMessage>
{
	private static Logger logger = LoggerFactory.getLogger(PubRecMessageHandler.class);

	private final NettyClient client;

	public PubRecMessageHandler(NettyClient client)
	{
		this.client = client;
	}

	@Override
	public void handle(MqttMessage message, Channel channel)
	{
		Object header = message.variableHeader();
		if (!(header instanceof MqttMessageIdVariableHeader))
		{
			logger.warn("MqttMessage.variableHeader is not a MqttMessageIdVariableHeader, type: {}",
					message.fixedHeader().messageType());
			return;
		}
		int messageId = ((MqttMessageIdVariableHeader) header).messageId();
		client.pubRec(messageId);
	}
}
